/**
 * 
 */
package furniture.render3d;

import javax.vecmath.Point2d;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;

/**
 * @author Grill Balazs (balage.g@gmail.com)
 *
 */
public class MatrixProjector implements Projector {

	private final Vector3d X;
	private final Vector3d Y;
	
	//private final Vector3d Z;
	
	/**
	 * Create a projector by giving a 2x3 matrix by two columns.
	 * @param X
	 * @param Y
	 */
	public MatrixProjector(Vector3d X, Vector3d Y){
		this.X = X;
		this.Y = Y;
		//Z = new Vector3d();
		//Z.cross(X, Y);
	}
	
	/**
	 * @see furniture.render3d.Projector#project(javax.vecmath.Point3d)
	 */
	public Point2d project(Point3d p) {
		Vector3d v = new Vector3d(p);
		Point2d q = new Point2d(v.dot(X),v.dot(Y));
		return q;
	}

	@Override
	public int compare(Polygon3d o1, Polygon3d o2) {
		Point3d p1 = o1.center();
		Point3d p2 = o2.center();
//		if (Math.abs(p1.z-p2.z) < 1e-2){
//			return Double.valueOf(p1.y).compareTo(Double.valueOf(p2.y));
//		}
//		return Double.valueOf(p1.z).compareTo(Double.valueOf(p2.z));
//		double d1 = Z.dot(new Vector3d(p1));
//		double d2 = Z.dot(new Vector3d(p2));
		double d1 = 2*p1.x+p1.y+p1.z;
		double d2 = 2*p2.x+p2.y+p2.z;
		return Double.valueOf(d1).compareTo(Double.valueOf(d2));
	}
	
}
